<?php
/**
 * 模块模型
 * BaseModule.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class BaseModule extends BaseModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BaseModule the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{base_module}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('module, module_name, url, version, description, setting', 'required'),
			array('disable, sort, createtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('module', 'length', 'max'=>15),
			array('module_name', 'length', 'max'=>20),
			array('url, version', 'length', 'max'=>50),
			array('description', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('module, module_name, url, version, disable, description, setting, sort, createtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
    /**
	 * 获取菜单列表
	 *
	 * @param array $params
	 * @return array 数据数组和分页对象
	 */
	public function getList($params)
	{
		$iPage = (isset($params['page']) && !empty($params['page'])) ? $params['page'] : 1;
		$iPagesize = (isset($params['pagesize']) && !empty($params['pagesize'])) ? $params['pagesize'] : Yii::app()->params->pageSize;

		$criteria = new CDbCriteria();
        $criteria->order = 'sort ASC' ;//排序条件 		
		$iCcount = self::model()->count($criteria);
		$oPages = new CPagination($iCcount);
		$oPages->pageSize = $iPagesize;
		$oPages->setCurrentPage($iPage-1);
		$oPages->applyLimit($criteria);
		$oResult = self::model()->findAll($criteria);

		$aList = array();
		if(!empty($oResult)){
			foreach ($oResult as $iKey => $aVal){
				$aList[$iKey]['module'] = $aVal->module;
                $aList[$iKey]['module_name'] = $aVal->module_name;
                $aList[$iKey]['url'] = $aVal->url;
                $aList[$iKey]['version'] = $aVal->version;
                $aList[$iKey]['disable'] = $aVal->disable;
                $aList[$iKey]['description'] = $aVal->description;
                $aList[$iKey]['setting'] = $aVal->setting;
				$aList[$iKey]['sort'] = $aVal->sort;
                $aList[$iKey]['createtime'] = $aVal->createtime;
                $aList[$iKey]['updatetime'] = $aVal->updatetime;
				//$list[$key]['tabletree'] = $this->getTreeTable($fidvalue."_".$value['area_id']);
			}
		}
		$listResult['list'] = $aList;
		$listResult['pages'] = $oPages;

		return $listResult;
	}
    /**
     * BaseModule::getOne() 获取一条信息
     * 
     * @param string $module 模块名称
     * @return array 当前模型名称
     */
    public function getOne($module){
        $oModel = $this->findByPk($module);
        return $oModel->attributes;
    }
    /**
     * BaseModule::saveData() 保存数据
     * 
     * @param mixed $attributes
     * @return mixed 成功返回主键  失败返回错误数组
     */
    public function saveData($attributes){
        $this->attributes=$attributes;
        if( $this->save() ){
            //保存成功能向menu表插入数据
            
            if( substr_count($attributes['url'] ,'/')!=0 ){
                //根据地址拆分动作
                if( substr_count($attributes['url'] ,'/')==1 ) $attributes['url'] = 'admin/' . $attributes['url'];
                $aAction = explode('/' ,$attributes['url']);
                $oMuneModel = AdminMenu::model()->find("module_id='". $aAction[0] ."' AND controller_id='" . $aAction[1] . "' AND action_id='". $aAction[2] ."'");
                $aMuneAttributes = array(
                    'menu_name' => $attributes['module_name'],
                    'module_id' => $aAction[0] ,
                    'controller_id' => $aAction[1] ,
                    'action_id' => $aAction[2] ,
                    'fid' => '88' ,
                    'display' => $attributes['disable']==1 ? 1 : 0 ,
                    'sort' => 0
                );
                if( $oMuneModel ){
                    $aMuneAttributes['pids'] = $oMuneModel->pids;
                    $oMuneModel->attributes = $aMuneAttributes;
                    $oMuneModel->save(false);                
                } else {
                    $oMuneModel = new AdminMenu();
                    $oMuneModel->attributes = $aMuneAttributes;
                    $oMuneModel->save(false); 
                    //更新pids
                    $sPids = ',87,88,' . $oMuneModel->menu_id .',';
                    $oMuneModel->pids = $sPids;
                    $oMuneModel->save(false);
                }
            }
            return $this->module;
        } else {
            return $this->errors;
        }
    }
    /**
     * BaseModule::deleteData() 删除数据
     * 
     * @param mixed $oModel
     * @return
     */
    public function deleteData($oModel){
        //获取对应栏目信息
        if( substr_count($oModel->url ,'/')!=0 ){
            //根据地址拆分动作
            if( substr_count($oModel->url ,'/')==1 ) $oModel->url = 'admin/' . $oModel->url;
            $aAction = explode('/' ,$oModel->url);
            $oMuneModel = AdminMenu::model()->find("module_id='". $aAction[0] ."' AND controller_id='" . $aAction[1] . "' AND action_id='". $aAction[2] ."'");
            //获取menu_id
            if( $oMuneModel ){
                $iMenuID = $oMuneModel->menu_id;
                AdminMenu::model()->deleteAll("FIND_IN_SET('".$iMenuID."' ,pids)");
            }
        }
        return $oModel->delete();
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'module' => '模块目录',
			'module_name' => '模块名称',
			'url' => '模块地址',
			'version' => '版本',
			'disable' => '禁用',
			'description' => '描述',
			'setting' => '设置',
			'sort' => '排序',
			'createtime' => '创建时间',
			'updatetime' => '更新时间'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('module',$this->module,true);
		$criteria->compare('module_name',$this->module_name,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('version',$this->version,true);
		$criteria->compare('is_enable',$this->is_enable);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('setting',$this->setting,true);
		$criteria->compare('order',$this->order);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('updatetime',$this->updatetime);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}